package utils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Calendar;

/**
 * Event logging class implements the Singleton pattern and writes event data to
 * the log file EventLog.txt
 */
public class Logger {

	private static Logger logger = new Logger();

	private Logger() {

	}

	/**
	 * @return the single instance of the Logger class
	 */
	public static Logger getLogger() {

		// check to see if static variable logger (an object of the Logger
		// class) has been instantiated
		if (logger == null) {
			// if not create a lock on the Logger class
			synchronized (Logger.class) {
				// check again to see if the variable logger is null
				if (logger == null) {
					// instantiate the Logger class
					logger = new Logger();
				}
			}

		}
		return logger;
	}

	/**
	 * 
	 * @param logData
	 *            - String containing Event data to be written to the file
	 *            EventLog.txt
	 */
	public void appendLog(String logData) {
		File file = new File("EventLog.txt");
		try {
			// append the message to the file
			FileWriter fw = new FileWriter(file, true);
			PrintWriter pw = new PrintWriter(fw);
			Calendar cal = Calendar.getInstance();

			String event = ("--------------------------------------------\n\n");
			event += ("Event logged at: " + cal.getTime() + "\n");
			event += (logData + "\n");
			event += ("\n");
			pw.print(event);
			fw.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
